package com.rois.librois.libRoISLog;

import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public final class MyFormatter extends Formatter {
	/**
	 * Log Format
	 * year/month/day/ time:minute:second.milsecond threadID log-level class [method] message
	 */
	@Override
	public synchronized String format(final LogRecord aRecord) {
		final StringBuffer message = new StringBuffer(131);

		// TIME
		long millis = aRecord.getMillis();
		String time = String.format("%tD %<tT.%<tL", millis);
		message.append(time);
		message.append(' ');

		// THREAD ID
		message.append(aRecord.getThreadID());
		message.append(' ');

		// LOG LEVEL
		message.append(aRecord.getLevel().toString());
		message.append(' ');

		// CLASS NAME
		String className = aRecord.getSourceClassName();
		message.append(className != null ? className : aRecord.getLoggerName());

		// METHOD NAME
		message.append(" [");
		String methodName = aRecord.getSourceMethodName();
		message.append(methodName != null ? methodName : "N/A");
		message.append("] ");

		// LOG MESSAGE
		message.append(formatMessage(aRecord));
		message.append('\n');

		Throwable throwable = aRecord.getThrown();
		if (throwable != null) {
			message.append(throwable.toString());
			message.append('\n');
			for (StackTraceElement trace : throwable.getStackTrace()) {
				message.append('\t');
				message.append(trace.toString());
				message.append('\n');
			}
		}
		return message.toString();
	}
}
